package com.yijinsuo.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ekikousei易皇星 on 16/11/22.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:  viewpager  滑动fragment懒加载
 */
public abstract class LazyFragment extends Fragment {
    protected View mRootView;
    public Context mContext;
    // 第一次可见时的状态
    private boolean firstVisible = true;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        Log.i("tag","setUserVisibleHint=====");
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            // 可见时执行的操作
            if (firstVisible) {
                firstVisibleInitData();
                firstVisible = false;
            }else {

                //

//				firstVisibleInitData();
            }
        } else {
            // 不可见的时候执行操作
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i("tag","onCreate=====");
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        setHasOptionsMenu(true);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i("tag","onCreateView=====");
        if (mRootView == null) {
            mRootView = initView();
            initData();
        }
        return mRootView;
    }



    public boolean isFirstVisible() {
        return firstVisible;
    }

    public void setFirstVisible(boolean firstVisible) {
        this.firstVisible = firstVisible;
    }

    public abstract View initView();

    public abstract void initData();
    /**
     * 第一次可见状态下加载数据的方法(且只会加载一次)
     */
    public  abstract  void firstVisibleInitData();

}
